/*
 * Copyright (C) 2011 Imavis Srl
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.imavis.video.parse.h264;

import com.imavis.video.parse.impl.BitBuffer;
import java.nio.ByteBuffer;

/**
 * Parser for SPS NAL unit.
 *
 * @author Andrea Gualano
 * @see H.264/MPEG-4 Part 10 §7.3.2.1
 */
class SequenceParameterSet {
    private int width;
    private int height;

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public static SequenceParameterSet parse(final ByteBuffer bb) {
        final BitBuffer b = new BitBuffer(bb);
        final SequenceParameterSet sps = new SequenceParameterSet();

        final int profile_idc = b.getBits(8);
        b.getBits(16);
        H264Utils.skipExpGolomb(b);
        if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122
                || profile_idc == 244 || profile_idc == 44 || profile_idc == 83
                || profile_idc == 86 || profile_idc == 118 || profile_idc == 128) {
            final int chroma_format_idc = H264Utils.getExpGolombUnsigned(b);
            if (chroma_format_idc == 3) {
                b.getBit();
            }
            H264Utils.skipExpGolomb(b);
            H264Utils.skipExpGolomb(b);
            b.getBit();
            final boolean seq_scaling_matrix_present_flag = b.getBit();
            if (seq_scaling_matrix_present_flag) {
                for (int i = 0; i < ((chroma_format_idc != 3) ? 8 : 12); i++) {
                    final boolean seq_scaling_list_present_flag = b.getBit();
                    if (seq_scaling_list_present_flag) {
                        final int sizeOfScalingList = (i < 6) ? 16 : 64;
                        int nextScale = 8;
                        for (int j = 0; j < sizeOfScalingList; j++) {
                            if (nextScale == 0) {
                                break;
                            }
                            int delta_scale = H264Utils.getExpGolombSigned(b);
                            nextScale = (nextScale + delta_scale + 256) % 256;
                        }
                    }
                }
            }
        }
        H264Utils.skipExpGolomb(b);
        final int pic_order_cnt_type = H264Utils.getExpGolombUnsigned(b);
        if (pic_order_cnt_type == 0) {
            H264Utils.skipExpGolomb(b);
        } else if (pic_order_cnt_type == 1) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        H264Utils.skipExpGolomb(b);
        b.getBit();

        final int pic_width_in_mbs_minus1 = H264Utils.getExpGolombUnsigned(b);
        sps.width = (pic_width_in_mbs_minus1 + 1) * 16;
        final int pic_height_in_map_units_minus1 = H264Utils.getExpGolombUnsigned(b);
        sps.height = (pic_height_in_map_units_minus1 + 1) * 16;
        final boolean frame_mbs_only_flag = b.getBit();
        if (!frame_mbs_only_flag) {
            sps.height *= 2;
        }

        return sps;
    }
}
